package com.yimi.policyservice.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.yimi.policyservice.common.constant.RedisConstant;
import com.yimi.policyservice.common.expetion.ApiException;
import com.yimi.policyservice.entity.base.system.SysUser;
import com.yimi.policyservice.repository.UserRepository;
import com.yimi.policyservice.service.SmsCodeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author jack
 */
@Service
public class SmsCodeServiceImpl implements SmsCodeService {

    @Autowired
    private UserRepository userRepository ;
    @Autowired
    private StringRedisTemplate stringRedisTemplate ;

    @Value("${qcloudsms.appId}")
    private int appId ;

    @Value("${qcloudsms.appKey}")
    private String appKey ;

    @Value("${qcloudsms.smsSign}")
    private String smsSign ;

    private static final int SMSCODE_TIMEOUT_MIN  = 10 ;

    private  static Logger LOGGER = LoggerFactory.getLogger(SmsCodeServiceImpl.class);

    @Override
    public void send(String phone,int tempId) {
        SysUser sysUser = userRepository.findFirstByTelephone(phone);
        if(sysUser == null ){
            throw  new ApiException(400,"您的该电话号码非平台注册号码");
        }
        String code = RandomUtil.randomNumbers(4);
        System.out.println(code);
        sendSms(phone,code,tempId);
        String cacheKey =String.format(RedisConstant.SMS_COKE_KEY,phone);
        stringRedisTemplate.opsForValue().set(cacheKey,code,SMSCODE_TIMEOUT_MIN, TimeUnit.MINUTES);

    }

    @Override
    public boolean verfiy(String phone, String code) {
        if(StringUtils.isBlank(phone) || StringUtils.isBlank(code)){
            return  false ;
        }
        String cacheKey = String.format(RedisConstant.SMS_COKE_KEY,phone);
        String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
        return  StringUtils.equals(code,cacheValue);
    }

    private void sendSms(String phone ,String content,int tempId){
        SmsSingleSender ssender = new SmsSingleSender(appId,appKey);
        try {
            SmsSingleSenderResult result = ssender.sendWithParam("86",phone,tempId,new String[]{content,"10"},smsSign,"","");
            LOGGER.debug("发送短信："+phone+" code:"+content+"result"+result);
        } catch (HTTPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public String code(String phone) {
        String cacheKey = String.format(RedisConstant.SMS_COKE_KEY,phone);
        String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
        return cacheValue;
    }


    @Override
    public void send4BindPhone(String phone) {

    }
}
